package com.feng.test.aatest;

import java.util.ArrayList;
import java.util.List;

/**
 * @author HuangSuip
 */
public class JavaInterview {
    public static void main(String[] args) {
        //
        //test1();
        test2();
    }

    static void test1() {
        String s = "1234";
        String s2 = "1232";

        int[] index = new int[s.length()];

        for (int i = 0; i < s.length(); i++) {
            index[i] = s2.indexOf(s2.charAt(i));
        }
        //System.out.println(Arrays.stream(index).map());
    }

    static void paile(String s, String p) {
        if (s.length() < 1) {

        } else {
            int[] index = new int[s.length()];
            for (int i = 0; i < s.length(); i++) {
                index[i] = s.indexOf(s.charAt(i));
            }
            for (int i = 0; i < s.length(); i++) {
                if (i == index[i]) {
                    paile(s.substring(1), s.substring(0, 1));
                }
                s = s.substring(1) + s.substring(0, 1);
            }
        }
    }
    
    static void test2(){
        ArrayList<Object> list = new ArrayList<>();
        int i = 0;
        long time1 = System.currentTimeMillis();
        long number = 1000 * 1000 * 50 ;
        while (i < number){
            i++;
            list.add(new Object());
        }
        System.out.println("Add time: " + (System.currentTimeMillis() - time1));
        final int[] max = {0};
/*        time1 = System.currentTimeMillis();
        System.out.println("start");
        for (Integer integer : list) {
            if (integer > max[0]){
                max[0] = integer;
            }
        }*/
        System.out.println("MAX: " + max[0]);
        System.out.println("foreach time: " + (System.currentTimeMillis() - time1));
        list = null;
        System.gc();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        long time2 = System.currentTimeMillis();
        List<Integer> list2 = new ArrayList<>();
        int j = 0;
        while ( j < number){
            j++;
            list2.add(j);
        }
        System.out.println("Add time: " + (System.currentTimeMillis() - time2));
        final int[] max2 = {0};
        time2 = System.currentTimeMillis();
        System.out.println("start");
        list2.forEach(n -> {
            if (n > max2[0]){
                max2[0] = n;
            }
        });
        System.out.println("MAX: " + max2[0]);
        System.out.println("foreach time: " + (System.currentTimeMillis() - time2));
    }
}
